/*****************************************************************************
  Copyright ?2002-2004 by Martin Cook. All rights are reserved. If you like
  this code then feel free to go ahead and use it. The only thing I ask is 
  that you don't remove or alter my copyright notice. Your use of this 
  software is entirely at your own risk. I make no claims about the 
  reliability or fitness of this code for any particular purpose. If you 
  make changes or additions to this code then please clearly mark your code 
  as yours. If you have questions or comments then please contact me at: 
  martin@codegator.com
  
  Have Fun! :o)
*****************************************************************************/

using System;
using System.Data;
using System.Threading;

using CG.Security.Configuration;
using CG.Security.Data;

namespace CG.Security
{

	/// <summary>
	/// Provides static methods that supply helper utilities for managing 
	/// system-level rights.
	/// </summary>
	public sealed class RightManager
	{
		
		// ******************************************************************
		// Attributes.
		// ******************************************************************

		#region Attributes

		/// <summary>
		/// The data object used to interface with the database.
		/// </summary>
		private static IRightData c_rightData;

		#endregion

		// ******************************************************************
		// Constructors.
		// ******************************************************************

		#region Constructors

		/// <summary>
		/// Class constructor.
		/// </summary>
		static RightManager()
		{
			c_rightData = DataManager.RightData;
		} // End RightManager()

		#endregion

		// ******************************************************************
		// Public methods.
		// ******************************************************************

		#region Public methods

		/// <summary>
		/// Creates a new system-level right. Note that the calling user must
		/// be a member of the 'Admin' role in order to perform this action.
		/// </summary>
		/// <param name="rightName">The name of the right (must be unique).</param>
		/// <param name="rightDescription">The description of the right (optional).</param>
		/// <returns>The identifier for the new right.</returns>
		public static int Create(
			string rightName,
			string rightDescription
			)
		{

			// Check the role of the user before we proceed.
			if (!Thread.CurrentPrincipal.IsInRole("Admin"))
				throw new SecurityException("User must be in the Admin role to perform this action!");

			// Create the right in the database.
			return c_rightData.Create(
				rightName,
				rightDescription
				);

		} // End Create()

		// ******************************************************************

		/// <summary>
		/// Deletes the specified system-level right.  Note that the calling user must
		/// be a member of the 'Admin' role in order to perform this action.
		/// </summary>
		/// <param name="rightID">The identifier for the right.</param>
		public static void Delete(
			int rightID
			)
		{

			// Check the role of the user before we proceed.
			if (!Thread.CurrentPrincipal.IsInRole("Admin"))
				throw new SecurityException("User must be in the Admin role to perform this action!");

			// Delete the right in the database.
			c_rightData.Delete(
				rightID
				);

		} // End Delete()

		// ******************************************************************

		/// <summary>
		/// Updates the specified system-level right.  Note that the calling user must
		/// be a member of the 'Admin' role in order to perform this action.
		/// </summary>
		/// <param name="rightID">The identifier for the right.</param>
		/// <param name="rightName">The new name for the right (must be unique).</param>
		/// <param name="rightDescription">The new description for the right (optional).</param>
		public static void Update(
			int rightID,
			string rightName,
			string rightDescription
			)
		{

			// Check the role of the user before we proceed.
			if (!Thread.CurrentPrincipal.IsInRole("Admin"))
				throw new SecurityException("User must be in the Admin role to perform this action!");

			// Update the right in the database.
			c_rightData.Update(
				rightID,
				rightName,
				rightDescription
				);
            
		} // End Update()

		// ******************************************************************

		/// <summary>
		/// Returns all the system-level rights in the database.
		/// </summary>
		/// <returns>A DataSet containing all the rights in the database.</returns>
		public static System.Data.DataSet FindAll()
		{

			// Get the list of rights from the database.
			DataSet ds = c_rightData.FindAll();

			// Ensure the dataset has a consistant name.
			ds.DataSetName = "RightData";

			// Ensure the table has a consistant name.
			ds.Tables[0].TableName = "cg_security_right";

			return ds;

		} // End FindAll()

		// ******************************************************************

		/// <summary>
		/// Returns the properties for the specified system-level right.
		/// </summary>
		/// <param name="rightID">The identifier for the right.</param>
		/// <returns>A DataReader containing the properties for the specified right.</returns>
		public static System.Data.IDataReader FindByPK(
			int rightID
			)
		{

			// Find the right in the database.
			return c_rightData.FindByPK(
				rightID
				);

		} // End FindByPK()

		#endregion

	} // End clas RightManager

} // End namespace CG.Security
